#include "sourcedetail.h"
#include "newspapermanager.h"
#include <QDebug>


QString SourceInfo::getUpdateFullNameImage(){
    qDebug() << m_fullNameImage;
    if(m_fullNameImage.length() == 0 )
    {
        // create
        QString icon(m_urlImageDomain.toString().data());
        icon.append("/");
        icon.append(NewspaperManager::instance()->dpiScreen());
        icon.append("/");
        icon.append(m_urlLink.toString());
        icon.append(".png");
        m_fullNameImage = icon;

    }
    return m_fullNameImage;
    //return m_fullNameImage;
}



SourceDetail::SourceDetail(int sourceId,
                           QString nameSource,
                           QUrl urlLinkSource,
                           QUrl urlSourceImageDomain,
                           QList < CategoryDetail* > listCategoryDetail,
                           QMap< int, CategoryDetail*> mapIdCategoryDetail,
                           QObject *parent)
    :QObject(parent),
    m_sourceInfo(SourceInfo(sourceId, nameSource, urlLinkSource, urlSourceImageDomain)),
    m_listCategoryDetail(listCategoryDetail),
    m_mapIdCategoryDetail(mapIdCategoryDetail)

{
}


CategoryDetail* SourceDetail::getCategoryDetailByIndex(const int idx)
{
    if(idx < m_listCategoryDetail.size())
    {
        return m_listCategoryDetail[idx];
    }
    return NULL;
}

CategoryDetail* SourceDetail::getCategoryDetailByKey(const int key)
{
    QMap<int, CategoryDetail*>::Iterator iter = m_mapIdCategoryDetail.find(key);
    if(iter!= m_mapIdCategoryDetail.end() && iter.key() == key)
    {
        return iter.value();
    }
    return NULL;
}


void SourceDetail::setCactegoryDetail(CategoryDetail *categoryDetail)
{
    int idCategory = categoryDetail->idCategory();
    CategoryDetail* oldCategory = getCategoryDetailByKey(idCategory);
    if(oldCategory == NULL){
        m_mapIdCategoryDetail[categoryDetail->idCategory()] = categoryDetail;
        m_listCategoryDetail.append(categoryDetail);
        return ;
    }
    qDebug() << "chua co category ";
}

int SourceDetail::getIndexByCategoryId(const int categoryId)
{
    if(m_listCategoryDetail.isEmpty())
    {
        return -1;
    }
    for(int i = 0; i< m_listCategoryDetail.size(); ++i){
        CategoryDetail* cDetail = m_listCategoryDetail.at(i);
        if(cDetail->idCategory() == categoryId){
            return i;
        }

    }
    return -1;
}

int SourceDetail::getCategoryIdByIndex(const int index)
{
    if(index > 0 && index < m_listCategoryDetail.size())
    {
        return m_listCategoryDetail[index]->idCategory();
    }
    return -1;
}
